package com.intech.android.demo.cms.beans;

import com.intech.android.demo.cms.bo.Group;
import com.intech.android.demo.cms.bo.Region;
import com.intech.android.demo.cms.common.Constants;
import com.intech.android.demo.cms.common.FacesHelper;
import com.intech.android.demo.cms.common.Util;
import com.intech.android.demo.cms.decorators.UserDecorator;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.List;

public class UserDbBean extends BaseBean
{
    private String old;
    private List<String> sexes;
    private String ARPU;
    private String newUserId;
    private String MSIDSN;
    private String activity;
    private String registrationDay;
    private String currentSex;
    private String currentRegion;
    private List<UserDecorator> userDecorators;
    private List<Integer> chosenFilterGroups = new ArrayList<Integer>();
    private List<Integer> chosenFilterRegions = new ArrayList<Integer>();
    private List<SelectItem> groups = new ArrayList<SelectItem>();
    private List<SelectItem> regions = new ArrayList<SelectItem>();

    public UserDbBean()
    {
        currentRegion = "Calcutta";
        currentSex = "male";
        sexes = new ArrayList<String>();
        sexes.add("male");
        sexes.add("female");
        fillUserListTempValues();
    }

    private void fillUserListTempValues()
    {
        userDecorators = new ArrayList<UserDecorator>();
        userDecorators.add(new UserDecorator("1", "9111111111", "male", "19", "5", "Mumbai", "active", "1500", "06.11.2011"));
        userDecorators.add(new UserDecorator("2", "9122222222", "female", "21", "20", "Delhi", "active", "350", "07.12.2011"));
        userDecorators.add(new UserDecorator("3", "9133333333", "male", "30", "17", "Bangalore", "inactive", "1140", "23.08.2011"));
        userDecorators.add(new UserDecorator("4", "9144444444", "female", "40", "8", "Calcutta", "active", "200", "23.10.2011"));
    }

    public List<UserDecorator> getUserDecorators()
    {
        return userDecorators;
    }

    public String saveEditUser()
    {
        return Constants.Navigation.SUBSCRIBERS_LINK;
    }

    public String toUserDB()
    {
        return Constants.Navigation.SUBSCRIBERS_LINK;
    }

    public String editUser()
    {

        Integer user_id = FacesHelper.getParameterAsInteger("edit_user");
        if (user_id == null)
        {
            return Constants.Navigation.SUBSCRIBERS_LINK;
        }
        fillAddUserForm(user_id);

        return Constants.Navigation.ADD_NEW_USER_LINK;
    }

    public String addUser()
    {
        currentRegion = null;
        currentSex = null;
        newUserId = "81";
        MSIDSN = "9155555555";
        activity = "inactive";
        registrationDay = null;
        old = null;
        ARPU = null;
        return Constants.Navigation.ADD_NEW_USER_LINK;
    }

    private void fillAddUserForm(Integer userNumber)
    {
        UserDecorator currentDecorator = new UserDecorator();
        for (UserDecorator userDecorator : userDecorators)
        {
            if (Integer.parseInt(userDecorator.getUserID()) == userNumber)
            {
                currentDecorator = userDecorator;
            }
        }

        currentRegion = currentDecorator.getRegion();
        currentSex = currentDecorator.getSex();
        newUserId = currentDecorator.getUserID();
        MSIDSN = currentDecorator.getMSISDN();
        activity = currentDecorator.getActivity();
        registrationDay = currentDecorator.getRegistrationDate();
        old = currentDecorator.getAge();
        ARPU = currentDecorator.getARPU();
    }

    public void setUserDecorators(List<UserDecorator> userDecorators)
    {
        this.userDecorators = userDecorators;
    }

    public String getCurrentRegion()
    {
        return currentRegion;
    }

    public void setCurrentRegion(String currentRegion)
    {
        this.currentRegion = currentRegion;
    }

    public String getCurrentSex()
    {
        return currentSex;
    }

    public void setCurrentSex(String currentSex)
    {
        this.currentSex = currentSex;
    }

    public String getOld()
    {
        return old;
    }

    public void setOld(String old)
    {
        this.old = old;
    }

    public List<String> getSexes()
    {
        return sexes;
    }

    public void setSexes(List<String> sexes)
    {
        this.sexes = sexes;
    }

    public List<SelectItem> getRegions()
    {
        if (Util.isEmpty(regions))
        {
            regions = new ArrayList<SelectItem>();
            List<Region> allRegions = getRegionDAO().getAll();
            for (Region region : allRegions)
            {
                SelectItem selectItem = new SelectItem(region.getId(), region.getName());
                regions.add(selectItem);
            }
        }
        return regions;
    }

    public void clearAllRegions(ActionEvent event)
    {
        chosenFilterRegions = new ArrayList<Integer>();
    }

    public void checkAllRegions(ActionEvent event)
    {
        chosenFilterRegions = new ArrayList<Integer>();
        for (SelectItem item : regions)
        {
            chosenFilterRegions.add((Integer) item.getValue());
        }
    }

    public void clearAllGroups(ActionEvent event)
    {
        chosenFilterGroups = new ArrayList<Integer>();
    }

    public void checkAllGroups(ActionEvent event)
    {
        chosenFilterGroups = new ArrayList<Integer>();
        for (SelectItem item : groups)
        {
            chosenFilterGroups.add((Integer) item.getValue());
        }
    }

    public void setRegions(List<SelectItem> regions)
    {
        this.regions = regions;
    }

    public List<SelectItem> getGroups()
    {
        if (Util.isEmpty(groups))
        {
            groups = new ArrayList<SelectItem>();
            List<Group> allGroups = getGroupDAO().getAll();
            for (Group group : allGroups)
            {
                SelectItem selectItem = new SelectItem(group.getId(), group.getName());
                groups.add(selectItem);
            }
        }
        return groups;
    }

    public String getNewUserId()
    {
        return newUserId;
    }

    public void setNewUserId(String newUserId)
    {
        this.newUserId = newUserId;
    }

    public String getMSIDSN()
    {
        return MSIDSN;
    }

    public void setMSIDSN(String MSIDSN)
    {
        this.MSIDSN = MSIDSN;
    }

    public String getRegistrationDay()
    {
        return registrationDay;
    }

    public void setRegistrationDay(String registrationDay)
    {
        this.registrationDay = registrationDay;
    }

    public String getActivity()
    {

        return activity;
    }

    public void setActivity(String activity)
    {
        this.activity = activity;
    }

    public String getARPU()
    {
        return ARPU;
    }

    public void setARPU(String ARPU)
    {
        this.ARPU = ARPU;
    }

    public List<Integer> getChosenFilterGroups()
    {
        return chosenFilterGroups;
    }

    public void setChosenFilterGroups(List<String> chosenFilterGroups)
    {
    }

    public List<Integer> getChosenFilterRegions()
    {
        return chosenFilterRegions;
    }

    public void setChosenFilterRegions(List<String> chosenFilterRegions)
    {
    }
}
